package MyGO.controller.MyController;

import MyGO.Data.GlobalData;
import MyGO.Main;
import MyGO.TableApplication;
import MyGO.controller.CheckDoctorViewController;
import MyGO.controller.CheckResultViewController;
import MyGO.controller.PrescriMedicationController;
import MyGO.controller.TableViewController;
import MyGO.utils.FileUtils.FXMLLoaderUtil;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import lombok.Getter;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

import static MyGO.controller.MyController.PushWindow.pushOnlyPrompt;

public class MyButton extends Button {
    private String title;
    private String url;

    @Getter
    private TableViewController tableViewController;

    /**
     * 构造方法
     * @param title : 按钮上显示的文字
     * @param imgName ：  按钮显示的图片的路径
     * @param viewName ：单击按钮是加载的view名称
     * @param doctor_main_r ：  目标容器
     */

    public MyButton(String title, String imgName, String viewName, AnchorPane doctor_main_r,
                    String label, String label_text,
                    String filtStatus, String filtDoctorID, String status,
                    String innerStatus) throws IOException {
        this.title = title;
        viewName = "/MyGO/fxml/" + viewName;
        label = "#" + label;
        // 获取图片
        File file = new File("src/main/resources/MyGO/image/" + imgName);
        String str = file.toURI().toString();
        Image image = new Image(str);
        ImageView view = new ImageView(image);
        view.setFitHeight(35);
        view.setFitWidth(30);
        this.setGraphic(view);   // 放入图片
        this.setText(title);     // 放上标题

        String finalViewName = viewName;
        String finalLabel = label;
        FXMLLoader loader = new FXMLLoader(Main.class.getResource(finalViewName));
        Pane pane = null;
        if (filtStatus != null && filtDoctorID != null) {
            if (Objects.equals(status, "1")) {
                loader.setControllerFactory(param -> new TableViewController(filtStatus, filtDoctorID, innerStatus));
            }
        } else if (filtStatus == null && filtDoctorID != null) {
            if (Objects.equals(status, "2")) {
                loader.setControllerFactory(param -> new CheckDoctorViewController(filtDoctorID));
            }
        } else if (filtStatus == null) {
            if (Objects.equals(status, "3")) {

            }
        }
        pane = loader.load();
        Pane finalPane = pane;

        // 设置点击事件
        setOnAction(event -> {
            try {
                if ((GlobalData.trackForm == null && Objects.equals(status, "2"))
                        || (GlobalData.trackForm == null && Objects.equals(status, "3"))
                        || (GlobalData.trackForm == null && Objects.equals(status, "4"))) {
                    pushOnlyPrompt("/MyGO/fxml/prompt_view_check_medicine.fxml", "请先叫号", 200.0, 100.0);
                } else {
                    //  Use lookup to find Label and textFild
                    assert finalPane != null;
                    if (Objects.equals(status, "2") || Objects.equals(status, "3") || Objects.equals(status, "4")) {
                        Label label1 = (Label) finalPane.lookup("#PnameW");
                        if (label1 != null) label1.setText(GlobalData.trackForm.getName());
                        Label label2 = (Label) finalPane.lookup("#PIDW");
                        if (label2 != null) label2.setText(GlobalData.trackForm.getId());
                        Label label3 = (Label) finalPane.lookup("#PageW");
                        if (label3 != null) label3.setText(String.valueOf(GlobalData.trackForm.getPatientInfoForm().getYear()));
                        Label label4 = (Label) finalPane.lookup("#PgenderW");
                        if (label4 != null) label4.setText(GlobalData.trackForm.getPatientInfoForm().getGender());
                    }

                    Label mylabel = (Label) finalPane.lookup(finalLabel);
                    if (mylabel != null) {
                        mylabel.setText(label_text);
                    }
                    finalPane.setLayoutX(0);
                    finalPane.setLayoutY(20);
                    doctor_main_r.getChildren().clear();
                    doctor_main_r.getChildren().add(finalPane);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        this.setStyle("-fx-font-size:24");
        this.setStyle("-fx-font-family: 'JetBrains Mono NL SemiBold'");
        this.setPrefWidth(200);
        this.setPrefHeight(68);
    }

}
